package com.health.trans.processor.reader;

import com.health.trans.annotation.Reader;
import com.health.trans.kit.ReaderConfig;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;

/**
 * @author nqlz
 */
@Reader
public class JdbcReader implements CsReader {

    /**
     * 让读者
     * 此方法会将数据一次性取出全部读到内存当中，造成内存大量消耗，无法分配内存的问题,不建议
     * JdbcCursorItemReader
     *
     * @param readerConfig 读者配置
     * @return {@link ItemReader}<{@link T}>
     */

    @Override
    public <T> ItemReader<T> getReader(ReaderConfig<T> readerConfig) {
        JdbcCursorItemReader<T> build = new JdbcCursorItemReaderBuilder<T>()
                .name(readerConfig.getTableClass().getSimpleName() + "ItemReader")
                .dataSource(readerConfig.getDataSource())
                .sql(readerConfig.getSql())
                .saveState(true)
                .fetchSize(500)
                .verifyCursorPosition(false)
                .beanRowMapper(readerConfig.getTableClass())
                .build();
        if (readerConfig.getMaxCount() > 0) {
            build.setMaxItemCount(readerConfig.getMaxCount());
        }
        return build;
    }

}
